package com.swiftops.basic.sys.service.impl;

import com.swiftops.common.constant.CommonEnum;
import com.swiftops.common.service.AbstractService;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.util.IdGen;
import com.swiftops.common.util.ListUtil;
import com.swiftops.common.constant.CommonConstant;
import com.swiftops.basic.sys.dao.SysMenuMapper;
import com.swiftops.basic.sys.model.SysMenu;
import com.swiftops.basic.sys.service.SysMenuService;
import com.swiftops.basic.sys.vo.entity.SysMenuEntityVO;
import com.swiftops.basic.sys.vo.request.SysMenuRequestVO;
import com.swiftops.basic.sys.vo.response.SysMenuResponseVO;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * 菜单
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@Service
@Transactional
public class SysMenuServiceImpl extends AbstractService<SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 获取菜单分页列表
     *
     * @param sysMenuRequestVO 菜单查询条件
     * @return pageResult 菜单分页结果
     */
    @Override
    public PageResult<SysMenuResponseVO> querySysMenuPageList(SysMenuRequestVO sysMenuRequestVO) {
        //
        if (sysMenuRequestVO.getPageNumber() == null) sysMenuRequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (sysMenuRequestVO.getPageSize() == null) sysMenuRequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<SysMenuResponseVO> sysMenuResponseVOList = Collections.emptyList();
        Condition condition = new Condition(SysMenu.class);
        Example.Criteria criteria = condition.createCriteria();
        //
        //if (StringUtils.isNotBlank(sysMenuRequestVO.getDesc())) {
        //    criteria.andLike("userName", "%" + sysMenuRequestVO.getDesc() + "%");
        //}
        //删除状态
        criteria.andEqualTo("deleteFlag", CommonEnum.StateFlagEnum.STATE_FLAG_NORMAL.getCode());
        //
        int total = this.selectCountByCondition(condition);
        if (total > 0) {
            PageHelper.startPage(sysMenuRequestVO.getPageNumber(), sysMenuRequestVO.getPageSize());
            List<SysMenu> sysMenuList = this.selectByCondition(condition);
            sysMenuResponseVOList = ListUtil.convertList(sysMenuList, SysMenuResponseVO.class);
        }
        //
        return PageResult.of(total, sysMenuResponseVOList);
    }

    /**
     * 获取菜单详细信息
     *
     * @param id 菜单ID
     * @return 菜单
     */
    @Override
    public SysMenuEntityVO getSysMenuEntityVO(String id) {
        SysMenu sysMenu = this.selectByPrimaryKey(id);
        if (null == sysMenu) return null;

        SysMenuEntityVO sysMenuEntityVO = new SysMenuEntityVO();
        BeanUtils.copyProperties(sysMenu, sysMenuEntityVO);
        return sysMenuEntityVO;
    }

    /**
     * 添加菜单
     *
     * @param sysMenuEntityVO 菜单
     */
    @Override
    public void addSysMenu(SysMenuEntityVO sysMenuEntityVO) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuEntityVO, sysMenu);
        sysMenu.setId(IdGen.next());
        this.insertSelective(sysMenu);
    }

    /**
     * 修改菜单
     *
     * @param sysMenuEntityVO 菜单
     */
    @Override
    public void updateSysMenu(SysMenuEntityVO sysMenuEntityVO) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuEntityVO, sysMenu);
        this.updateByPrimaryKeySelective(sysMenu);
    }

    /**
     * @param id        主键
     * @param isUseable 启用状态
     */
    @Override
    public void useableSysMenu(String id, Boolean isUseable) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(id);
        Byte useable = isUseable ? CommonEnum.EnableStateEnum.ENABLE_STATE_YES.getCode() : CommonEnum.EnableStateEnum.ENABLE_STATE_NO.getCode();
        sysMenu.setUseable(useable);
        this.updateByPrimaryKeySelective(sysMenu);
    }

    /**
     * 删除菜单
     *
     * @param id 菜单ID
     */
    @Override
    public void deleteSysMenu(String id) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(id);
        //sysMenu.setStateFlag(CommonEnum.StateFlagEnum.STATE_FLAG_DELETE.getCode());
        //逻辑删除
        this.updateByPrimaryKeySelective(sysMenu);
    }
}
